﻿#define Test
// See https://aka.ms/new-console-template for more information
using DidaCode.Workflow;
using DidaCode.Workflow.Models;
using DidaCode.Workflow.Models.Parameters;
using DidaCode.Workflow.Models.Results;
using DidaCode.Workflow.NodeBase;
using DidaCode.Workflow.Nodes;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Internal;
using System.Text.Json;
using System;
using System.Text.Json.Serialization;
using System.Xml.Linq;
using Microsoft.Extensions.Options;
using System.Text.Encodings.Web;
using DidaCode.Workflow.Logs;
using System.Diagnostics;
using DidaCode.Workflow.Help;


var configuration = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json")
           .Build();


configuration.GetSection("Nodes").Bind(NodeConfig.RootConfig);

// 创建节点树
#if Test
List<Task> taskList = new List<Task>();
for (int i = 0; i < 3; i++)
{
    taskList.Add(new Workflow<HotelSearchParameter, HotelSearchResult>().ExecuteAsync(new HotelSearchParameter { HotelId = i }));
}
await Task.WhenAll(taskList);


//var hotelIds = Enumerable.Range(0, 3);
//await Parallel.ForEachAsync(hotelIds, async (hotelId, _) =>
//{
//    var workflow = new Workflow<HotelSearchParameter, HotelSearchResult>();
//    await workflow.ExecuteAsync(new HotelSearchParameter { HotelId = hotelId });
//});
#else
//执行工作流
var parameter = new HotelSearchParameter
{
    HotelId = 1
};
 var workflow = new Workflow<HotelSearchParameter, HotelSearchResult>();
var result = await workflow.ExecuteAsync(parameter);
if (result?.IsSuccess == true)
{
    Console.WriteLine($"Final Result: {JsonSerializer.Serialize(result.Value, 
        new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping })} ");
}
#endif




